package com.lastproject.web.bean;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

import com.lastproject.dto.EmailGroupDTO;
import com.lastproject.dto.UserDTO;
import com.lastproject.ejb.dao.CheckPermissionDAORemote;
import com.lastproject.ejb.dao.EmailGroupDAORemote;
import com.lastproject.ejb.dao.UserDAORemote;

public class AddUserToGroupBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2227231136789793432L;
	
	private int id;
	
	UserDTO userDTO;
	
	EmailGroupDTO emailGroupDTO;
	
	List<UserDTO> userDTOList = new ArrayList<UserDTO>();
	
	@EJB
	EmailGroupDAORemote emailGroupDAORemote;
	
	@EJB
	UserDAORemote userDAORemote;
	
	public AddUserToGroupBean(){
		
	}
	
	@PostConstruct
	public void met(){
		this.userDTOList = userDAORemote.getAll();
	}
	
	@EJB
	CheckPermissionDAORemote checkPermissionDAORemote;
	
	public void preRenderView(ComponentSystemEvent event) {
        if (!checkPermissionDAORemote.hasSurveyManagementRights(true)){
        	FacesContext fc = FacesContext.getCurrentInstance();
        	ExternalContext ec = fc.getExternalContext();
        	try {
				ec.redirect("restrictedAccess.xhtml");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    }
	
	/**
	 * add User to Group
	 * @return Returns a string containing [nextPage].xhtml
	 */
	public String addUser(){
		userDTO = userDAORemote.findById(id);
		emailGroupDTO = emailGroupDAORemote.findLastByName(emailGroupDTO.getName());
		List<UserDTO> users = emailGroupDTO.getUsers();
		if (users == null){
			users = new ArrayList<UserDTO>();
		}
		users.add(userDTO);
		emailGroupDTO.setUsers(users);
		emailGroupDAORemote.edit(true,emailGroupDTO);
		List<UserDTO> newList = new ArrayList<UserDTO>();
		for (int i = 0; i < userDTOList.size(); i++){
			if (userDTOList.get(i).getId() != userDTO.getId())
				newList.add(userDTOList.get(i));
		}
		userDTOList = newList;
		return "addUserToGroup.xhtml";
	}

	public UserDTO getUserDTO() {
		return userDTO;
	}

	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}

	public EmailGroupDTO getEmailGroupDTO() {
		return emailGroupDTO;
	}

	public void setEmailGroupDTO(EmailGroupDTO emailGroupDTO) {
		this.emailGroupDTO = emailGroupDTO;
	}

//	public List<UserDTO> getUserDTOList() {
//		userDTOList = userDAORemote.getAll();
//		List<UserDTO> newList = new ArrayList<UserDTO>();
//		emailGroupDTO = emailGroupDAORemote.findLastByName(emailGroupDTO.getName());
//		for (UserDTO u:userDTOList){
//			boolean b = true;
//			for (int i = 0; i < emailGroupDTO.getUsers().size(); i++){
//				if (u.equals(emailGroupDTO.getUsers().get(i))){
//					b = false;
//				}
//			}
//			if (b == true)
//				newList.add(u);
//		}
//		return newList;
//	}
	public List<UserDTO> getUserDTOList() {
		return userDTOList;
	}

	public void setUserDTOList(List<UserDTO> userDTOList) {
		this.userDTOList = userDTOList;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}
